﻿package 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import screens.NewSongScreen;
	import screens.TitleScreen;
	import flash.display.StageScaleMode;
	import screens.Screen;
	import flash.utils.getTimer;
	[SWF(width = '800', height = '470')];
	
	/**
	 * ...
	 * @author Andy Wolff
	 */
	public class Main extends Sprite 
	{
		private var screen:Screen;
		static public var instance:Main;
		
		public function Main():void 
		{
			instance = this;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			instance = this;
			switchScreen(new TitleScreen(this));
		}
		
		public function switchScreen(newScreen:Screen):void
		{
			if (screen != null) { screen.end(); stage.removeChild(screen); }
			stage.addChild(newScreen);
			screen = newScreen;
		}
		
		/**
		 * Centers the position of the given movie clip according to the screen width of 800 and the clip's width.
		 * @param	mc the movie clip to be centered.
		 */
		static public function center(mc:MovieClip):void { mc.x = 400 - mc.width / 2; mc.y = 470 / 2 - mc.height / 2; }
		static public function center2(mc:MovieClip, width:Number, height:Number):void { mc.x = width/2 - mc.width / 2; mc.y = height / 2 - mc.height / 2; }
		
		static public function sleep(ms:int):void {
			var startTime:int = getTimer();
			while (true) {
			if (getTimer() - startTime >= ms) break;
			}
		}
	}
	
}